home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 4_10.lha / 4_10 / 4_10c.c < prev    next >
Text File  |  1993-08-08  |  963b  |  45 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / Exercise 4.10 (version 2)
  6. / Invert a two dimensional (n X n) array
  7. / using the Gauss-Jordan elimination method
  8. / with partial pivoting.
  9. include <swap.h>
  10. include <abs.h>
  11.  
  12. / Find the maximum |A[i][diag]|, i >= diag.
  13. / Return FALSE if there are no non-zero entries.
  14. tatic int dopivot(double **A, double **I,
  15.    int diag, int nelem)
  16.  
  17.    double max = abs(A[diag][diag]);
  18.    int swaprow = diag;
  19.  
  20.    for (int i = diag+1; i < nelem; i++)
  21. {
  22. double a_i = abs(A[i][diag]);
  23. if (a_i > max)
  24.     {
  25.     max = a_i;
  26.     swaprow = i;
  27.     }
  28. }
  29.  
  30.    // if all are zero, then failure
  31.    if (max == 0.0)
  32. return 0;
  33.  
  34.    // swap row swaprow with row diag in A and I
  35.    if (swaprow != diag)
  36. {
  37. for (int j = diag; j < nelem; j++)
  38.     swap(A[diag][j], A[swaprow][j]);
  39. for (j = 0; j < nelem; j++)
  40.     swap(I[diag][j], I[swaprow][j]);
  41. }
  42.  
  43.    return 1;
  44.  
  45.